package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrVo;
import com.atguigu.gulimail.product.vo.SkuInfoVo;
import com.atguigu.gulimail.product.vo.SpuItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrAttrgroupRelationService relationService;

    @Autowired
    AttrService attrService;
    @Autowired
    AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageWithId(Map<String, Object> params, Long cateLogId) {
        //在这里  查询条件首先封装好一个模糊查询。  然后 在根据有没有callogId 如过没有 查询所有带上模糊查询的条件。如果有。在继续加上查询条件。
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            //  如果有，那就要进行多字段的模糊查询
            // select * from xx where catelog_id  =catLogId and (attr_group_id = key or attr_group_name like key
            queryWrapper.and((obj) -> {
                obj.like("attr_group_id", key).or().like("attr_group_name", key);
            });
            //如果没有选出三级分类。那么三级分类id=0;  查出所有
            if (cateLogId == 0) {
                IPage<AttrGroupEntity> page =
                        this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
                return new PageUtils(page);
            } else {
                //首先判断有没有检索的参数。
                System.out.println("首先判断有没有检索的参数。");
                queryWrapper.eq("catelog_id", cateLogId);

                IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
                return new PageUtils(page);
            }
        } else {
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<>());
            return new PageUtils(page);
        }


    }

    //获取当前分类下所有属性分组的属性信息
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {

        //首先根据当前分类 获取所有属性分组的集合
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //获取当前分组集合的所有attr_group_id

        List<AttrGroupWithAttrVo> withAttrVos = groupEntities.stream().map(item -> {
            AttrGroupWithAttrVo withAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(item, withAttrVo);
            List<AttrEntity> attrsByGroupId = attrService.getAttrsByGroupId(withAttrVo.getAttrGroupId());
            withAttrVo.setAttrs(attrsByGroupId);
            return withAttrVo;
        }).collect(Collectors.toList());


        return withAttrVos;
    }

    @Override
    public List<SpuItemGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息。以及当前分组下所有属性对应的值。
        List<SpuItemGroupVo> spuItemGroupVos = attrGroupDao.listSpuGroupInfo(spuId, catalogId);

        return spuItemGroupVos;
    }


}